//
//  MYGHealthManagementTopTab.swift
//  MYGrow
//
//  Created by 林矗 on 2024/3/9.
//

import UIKit

class MYGHealthManagementTopTab: UIView {
    // MARK: - Property
    var contents: [MYGHealthManagementTopTabCellModel] = []
    var selectedIndex = 0 {
        didSet {
            if selectedIndex > 3 {
                selectedIndex = 3
            }
            collectionView.reloadData()
        }
    }
    var itemDidSelectAction: ((Int) -> ())?
    private var containerWidth: CGFloat {
        let width = (screenWidth - 12 * 5) / 4.0
        return width
    }
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
        configItem()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo(containerWidth + 20)
        }
    }
    
    private func configItem() {
        let statisticsModel = MYGHealthManagementTopTabCellModel(type: .statistics)
        let habitModel = MYGHealthManagementTopTabCellModel(type: .habit)
        let historyModel = MYGHealthManagementTopTabCellModel(type: .history)
        let reportModel = MYGHealthManagementTopTabCellModel(type: .report)
        contents = [statisticsModel, habitModel, historyModel, reportModel]
        collectionView.reloadData()
    }
    
    // MARK: - Public funcs
    
    // MARK: - lazy load
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(cellWithClass: MYGHealthManagementTopTabCell.self)
        return collectionView
    }()
}

extension MYGHealthManagementTopTab: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSizeMake(containerWidth, containerWidth)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        12
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        12
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        UIEdgeInsets(top: 20, left: 12, bottom: 0, right: 12)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        contents.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withClass: MYGHealthManagementTopTabCell.self, for: indexPath)
        var model = contents[indexPath.row]
        model.selected = selectedIndex == indexPath.row
        cell.model = model
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row == selectedIndex { return }
        selectedIndex = indexPath.row
        itemDidSelectAction?(selectedIndex)
        collectionView.reloadData()
    }
    
    
}
